//
//  STFeedListStoryWithImageCell.swift
//  Story
//
//  Created by 汤泽川 on 2019/4/19.
//  Copyright © 2019 汤泽川. All rights reserved.
//

import UIKit

class STFeedListStoryWithImageCell: UITableViewCell {
    private let containerView : UIView = {
        let view = UIView()
        view.layer.shadowColor = UIColor.init(white: 0, alpha: 0.1).cgColor
        view.layer.shadowOpacity = 0.6;
        view.layer.shadowRadius = 2;
        view.layer.shadowOffset = CGSize(width: 0, height: 0)
        view.backgroundColor = UIColor.white
        view.layer.cornerRadius = 4
        view.layer.masksToBounds = true
        return view
    }()
    
    private let storyImage = UIImageView()
    
    private let titleLabel: UILabel = {
        let label = UILabel()
        label.textColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)
        label.font = UIFont.systemFont(ofSize: 13)
        label.numberOfLines = 1
        return label
    }()
    
    private let authorUsername : UILabel = {
        let label = UILabel()
        label.textColor = #colorLiteral(red: 0.501960814, green: 0.501960814, blue: 0.501960814, alpha: 1)
        label.font = UIFont.systemFont(ofSize: 11)
        return label
    }()

    private let introduceLabel : UILabel = {
        let label = UILabel()
        label.textColor = #colorLiteral(red: 0.2549019754, green: 0.2745098174, blue: 0.3019607961, alpha: 1)
        label.font = UIFont.systemFont(ofSize: 10)
        label.numberOfLines = 2
        return label
    }()
    
    private let preferenceLabel : UILabel = {
        let label = UILabel()
        label.textColor = #colorLiteral(red: 0.2549019754, green: 0.2745098174, blue: 0.3019607961, alpha: 1)
        label.font = UIFont.italicSystemFont(ofSize: 8)
        return label
    }()
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.contentView.backgroundColor = UIColor.init(white: 0, alpha: 0.03)
        self.contentView.addSubview(containerView)
        self.containerView.addSubview(storyImage)
        self.containerView.addSubview(titleLabel)
        self.containerView.addSubview(authorUsername)
        self.containerView.addSubview(introduceLabel)
        self.containerView.addSubview(preferenceLabel)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    public func configureCell(storyItem : STStoryInfo) {
        self.titleLabel.text = storyItem.storyInfo.storyName
        self.authorUsername.text = storyItem.authorInfo.username
        self.preferenceLabel.text = "\(storyItem.collectionCount) 收藏 · \(storyItem.commentCount) 评论"
        self.storyImage.kf.setImage(with: URL(string: storyItem.storyInfo.avatarUrl ?? ""), placeholder:UIImage(color: UIColor.gray))
        self.introduceLabel.attributedText = mapToAttr(str:storyItem.storyInfo.introduce)
        self.setNeedsUpdateConstraints()
    }
    
    override func updateConstraints() {
        super.updateConstraints()
        self.containerView.snp.updateConstraints { (make) in
            make.left.right.equalToSuperview().inset(4)
            make.bottom.equalToSuperview().offset(-4)
            make.top.equalToSuperview()
        }
        
        self.storyImage.snp.updateConstraints { (make) in
            make.top.right.bottom.equalToSuperview()
            make.size.equalTo(CGSize(width: 177, height: 100))
        }
        
        self.titleLabel.snp.updateConstraints { (make) in
            make.left.equalToSuperview().offset(6)
            make.top.equalToSuperview().offset(12)
            make.right.equalTo(self.storyImage.snp_left).offset(-10)
        }

        self.authorUsername.snp.updateConstraints { (make) in
            make.left.equalToSuperview().offset(6)
            make.bottom.equalToSuperview().offset(-4)
        }

        self.introduceLabel.snp.updateConstraints { (make) in
            make.left.right.equalTo(self.titleLabel)
            make.top.equalTo(self.titleLabel.snp_bottom).offset(8)
        }
        
        self.preferenceLabel.snp.updateConstraints { (make) in
            make.left.equalTo(self.authorUsername.snp_right).offset(8)
            make.bottom.equalTo(self.authorUsername.snp_bottom)
        }
    }
    
    private func mapToAttr(str : String) -> NSAttributedString {
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineSpacing = 3
        paragraphStyle.lineBreakMode = NSLineBreakMode.byTruncatingTail
        let attr = NSAttributedString(string: str, attributes: [NSAttributedString.Key.kern : 1, NSAttributedString.Key.paragraphStyle : paragraphStyle, NSAttributedString.Key.font : UIFont.systemFont(ofSize: 11), NSAttributedString.Key.foregroundColor : #colorLiteral(red: 0.2549019754, green: 0.2745098174, blue: 0.3019607961, alpha: 1)])
        return attr
    }
}
